package com.hsav.kcxb.fragment.sing;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.hsav.kcxb.R;
import com.hsav.kcxb.adapter.ContributeAdapter;
import com.hsav.kcxb.adapter.GiftListAdapter;

import java.util.Arrays;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link GiftFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link GiftFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GiftFragment extends Fragment implements TabHost.OnTabChangeListener, View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private RecyclerView listGift;
    private RecyclerView listGiftReceived;
    private ListView listContribution;
    private int giftCountSelected;
    private TextView textGiftCountSelected;

    public GiftFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GiftFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GiftFragment newInstance(String param1, String param2) {
        GiftFragment fragment = new GiftFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift, container, false);
        TabHost tabHost = (TabHost) view.findViewById(R.id.tab_host);
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("gift").setContent(R.id.tab_gift).setIndicator("礼物"));
        tabHost.addTab(tabHost.newTabSpec("contribution").setContent(R.id.tab_contribution).setIndicator("贡献榜"));
        tabHost.addTab(tabHost.newTabSpec("gift_received").setContent(R.id.tab_gift_received).setIndicator("收到的礼物"));
        tabHost.setOnTabChangedListener(this);
        listGift = (RecyclerView)view.findViewById(R.id.list_gift);
        setupGiftList();
        listContribution = (ListView)view.findViewById(R.id.list_contribution);
        listContribution.setAdapter(new ContributeAdapter(getActivity()));
        listGiftReceived = (RecyclerView)view.findViewById(R.id.list_gift_received);
        setupGiftReceivedList();
        textGiftCountSelected = (TextView)view.findViewById(R.id.text_gift_count_selected);
        textGiftCountSelected.setText("1");
        View viewToSelectCount = view.findViewById(R.id.view_to_select_count);
        viewToSelectCount.setOnClickListener(this);
        TextView textToCharge = (TextView) view.findViewById(R.id.text_to_charge);
        textToCharge.setOnClickListener(this);
        TextView textToPresent = (TextView) view.findViewById(R.id.text_to_present);
        textToPresent.setOnClickListener(this);
        ImageView imgClose = (ImageView) view.findViewById(R.id.img_close);
        imgClose.setOnClickListener(this);
        return view;
    }

    private void setupGiftReceivedList() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
        listGiftReceived.setLayoutManager(gridLayoutManager);
        GiftListAdapter giftListAdapter = new GiftListAdapter(getActivity());
        listGiftReceived.setAdapter(giftListAdapter);
    }

    private void setupGiftList() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);
        listGift.setLayoutManager(gridLayoutManager);
        GiftListAdapter giftListAdapter = new GiftListAdapter(getActivity());
        listGift.setAdapter(giftListAdapter);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onTabChanged(String tabId) {
        Toast.makeText(getActivity(), String.format("Tab changed.(%s)", tabId), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.text_to_charge:
                // TODO: 2017/6/22
                Toast.makeText(getActivity(), "充值", Toast.LENGTH_SHORT).show();
                break;
            case R.id.view_to_select_count:
                showCountListDialog();
                break;
            case R.id.text_to_present:
                // TODO: 2017/6/22
                Toast.makeText(getActivity(), "赠送", Toast.LENGTH_SHORT).show();
                break;
            case R.id.img_close:
                if (mListener != null)
                    mListener.onCloseButtonClicked();
                break;
        }
    }

    private void showCountListDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("选择数量");
        final String[] items = new String[]{ "1", "5", "10", "15", "50", "100" };
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                giftCountSelected = Integer.valueOf(items[which]);
                textGiftCountSelected.setText(items[which]);
                Toast.makeText(getActivity(), String.format("选择了%s", items[which]), Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onCloseButtonClicked();
    }
}
